<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>首页 - 华鑫农业后台管理系统后台管理系统</title>
<link rel="icon" href="favicon.ico" type="image/ico">
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/materialdesignicons.min.css" rel="stylesheet">
<link href="../css/style.min.css" rel="stylesheet">
  <script type="text/javascript" src="../js/jquery.min.js"></script>
<%--  <script type="text/javascript" src="../js/template-web.js"></script>--%>
</head>

<body>
<div class="lyear-layout-web">
  <div class="lyear-layout-container">
    <%--    菜单栏--%>
    <jsp:include page="left_nav.jsp"/>
    <%--头部--%>
    <jsp:include page="header.jsp"/>

      <script type="text/javascript">

        for (let i = 1; i <= 6; i++) {
          $("#nav" + i).attr("class", "nav-item");
        }
        $("#nav5").attr("class", "nav-item active");
      </script>
      <!--页面主要内容-->
      <main class="lyear-layout-content">

        <div class="container-fluid">

          <div class="row">
            <div class="col-lg-12">
              <div class="card">
                <div class="card-toolbar clearfix">

                  <%--                搜索功能的页面--%>
                  <form class="pull-right search-bar" method="get" action="#!" role="form">
                    <div class="input-group">
                      <div class="input-group-btn">
                        <input type="hidden" name="search_field" id="search-field" value="title">
                        <button class="btn btn-default dropdown-toggle" id="search-btn" data-toggle="dropdown" type="button" aria-haspopup="true" aria-expanded="false">
                          内容 <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu">
                          <li> <a tabindex="-1" href="javascript:void(0)" data-field="title">内容</a> </li>
                          <li> <a tabindex="-1" href="javascript:void(0)" data-field="cat_name">时间</a> </li>
                        </ul>
                      </div>
                      <input type="text" class="form-control" value="" name="keyword" placeholder="请输入名称">
                    </div>
                  </form>
                  <div class="toolbar-btn-action">
                    <a class="btn btn-primary m-r-5" href="#!" data-toggle="modal" data-target="#myModal"><i class="mdi mdi-plus" ></i> 发布</a>
                  </div>
                </div>
                <div class="card-body">

                  <div class="table-responsive">
                    <table class="table table-bordered">
                      <thead>
                      <tr>

                        <th>编号</th>
                        <th>新闻标题</th>
                        <th>发布时间</th>
                        <th>操作</th>
                      </tr>
                      </thead>
                      <tbody id="tb_data">
                      </tbody>
                    </table>
                  </div>


                  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                    <div class="modal-dialog" role="document">
                      <div class="modal-content">
                        <div class="modal-header">
                          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                          <h4 class="modal-title" id="myModalLabel">发布农业新闻</h4>
                        </div>
                        <div class="modal-body">
                          <label class="col-xs-12" for="example-text-input">新闻标题</label>
                          <input class="form-control" type="text" id="example-text-input" name="example-text-input" placeholder="请输入新闻标题..">
                          <label class="col-xs-12" for="example-text-input">详细内容</label>
                          <textarea class="form-control" id="example-textarea-input" name="example-textarea-input" rows="6" placeholder="请输入新闻详细内容.."></textarea>
                        </div>
                        <div class="modal-footer">
                          <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                          <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="addNewsItem()">点击发布</button>
                        </div>
                      </div>
                    </div>
                  </div>

                  <div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                    <div class="modal-dialog" role="document">
                      <div class="modal-content">
                        <div class="modal-header">
                          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                          <h4 class="modal-title" id="myModalLabel2">查看内容详情</h4>
                        </div>
                        <div class="modal-body">
                          <label class="col-xs-12" for="example-text-input" id="new_title"></label>
                          <textarea class="form-control" id="example-textarea-show" name="example-textarea-input" rows="6" disabled="disabled"></textarea>                        </div>
                        <div class="modal-footer">
                          <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        </div>
                      </div>
                    </div>
                  </div>


                  <div class="modal fade" id="myModal3" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                    <div class="modal-dialog" role="document">
                      <div class="modal-content">
                        <div class="modal-header">
                          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                          <h4 class="modal-title" id="myModalLabel3">发布农业新闻</h4>
                        </div>
                        <div class="modal-body">
                          <label class="col-xs-12" for="example-text-input">新闻标题</label>
                          <input class="form-control" type="text" id="text-updata" name="example-text-input" placeholder="请输入新闻标题..">
                          <label class="col-xs-12" for="example-text-input">详细内容</label>
                          <textarea class="form-control" id="textarea-updata" name="example-textarea-input" rows="6" placeholder="请输入新闻详细内容.."></textarea>
                        </div>
                        <div class="modal-footer">
                          <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                          <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="upDataNewsItem()">点击修改</button>
                        </div>
                      </div>
                    </div>
                  </div>
                  <%--                分页，先不实现--%>
                  <%--                <ul class="pagination">--%>
                  <%--                  <li class="disabled"><span>«</span></li>--%>
                  <%--                  <li class="active"><span>1</span></li>--%>
                  <%--                  <li><a href="#1">2</a></li>--%>
                  <%--                  <li><a href="#1">3</a></li>--%>
                  <%--                  <li><a href="#1">4</a></li>--%>
                  <%--                  <li><a href="#1">5</a></li>--%>
                  <%--                  <li><a href="#1">6</a></li>--%>
                  <%--                  <li><a href="#1">7</a></li>--%>
                  <%--                  <li><a href="#1">8</a></li>--%>
                  <%--                  <li class="disabled"><span>...</span></li>--%>
                  <%--                  <li><a href="#!">14452</a></li>--%>
                  <%--                  <li><a href="#!">14453</a></li>--%>
                  <%--                  <li><a href="#!">»</a></li>--%>
                  <%--                </ul>--%>

                </div>
              </div>
            </div>

          </div>

        </div>

      </main>
      <!--End 页面主要内容-->
  </div>
</div>


<script type="text/javascript" src="../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="../js/main.min.js"></script>
<script src="../js/myScript/app.js"></script>
<script type="text/javascript">

  function queryAllNews() {
    $.ajax({
      type: "get",
      async: true,
      url: serverUrl + "/news/queryNews",
      dataType: "json",
      success: function (res) {
        if (res.success) {
          //请求成功
          var resData = res.data;
          for (let i = 0; i < resData.length; i++) {
            var item = resData[i];

            $("#tb_data").append("<tr>\n" +
                    "                      <td id=\"" + item.nkId + "\">" + item.nkId + "</td>\n" +
                    "                      <td>" + item.nkTitle + "</td>\n" +
                    "                      <td>" + item.nkTime + "</td>\n" +
                    "                      <td>\n" +
                    "                        <div class=\"btn-group\">\n" +
                    "                          <a id=\'" + item.nkId + "\' class=\"btn btn-xs btn-default\" href=\"#!\" title=\"修改\" data-toggle=\"modal\" data-target=\"#myModal3\" onclick='queryNews2(this)'><i class=\"btn btn-sm btn-primary\">编辑</i></a>\n" +
                    "                          <a id=\'" + item.nkId + "\' class=\"btn btn-xs btn-default\" href=\"#!\" title=\"查看\"  data-toggle=\"modal\" data-target=\"#myModal2\" onclick='queryNews(this)'><i class=\"btn btn-sm btn-primary\">查看</i></a>\n" +
                    "                          <a id=\'" + item.nkId + "\' class=\"btn btn-xs btn-default\" href=\"#!\" title=\"删除\" data-toggle=\"tooltip\" onclick='deleteNewsItem(this)'><i class=\"btn btn-sm btn-danger\">删除</i></a>\n" +
                    "                        </div>\n" +
                    "                      </td>\n" +
                    "                    </tr>");

          }
        } else {
          alert(res.msg);
        }

      },
      error: function (res) {
        alert("请求出错");
      }
    });
  }

  //加载页面请求
  queryAllNews();

  /**
   * 新增功能
   * @param cyId
   */
  function addNewsItem() {
    //修改
    var title = $("#example-text-input").val();
    var content = $("#example-textarea-input").val();
    if (title == null || title === "" || content == null || content === "") {
      alert("标题或内容不能为空");
      return;
    }
    $.ajax({
      type: "POST",
      contentType: "application/json;charset=utf-8",
      url: serverUrl + "/news/addNews",
      data: JSON.stringify({"nkTitle":title, "nkMessage":content}),
      dataType: "json",
      success: function (res) {
        $("#tb_data").empty();
        queryAllNews();
      },
      error: function (res) {
        alert("请求错误");
      }
    });
    $("#example-text-input").val("");
    $("#example-textarea-input").val("");
  }

  /**
   * 修改指定的简介
   */
  function upDataNewsItem() {

    //修改的id号
    var id = $("#text-updata").attr("name");
    var title = $("#text-updata").val();
    var content = $("#textarea-updata").val();

    $.ajax({
      type: "POST",
      contentType:"application/json;charset=UTF-8",
      url: serverUrl + "/news/updateNews",
      data: JSON.stringify({"nkId": id, "nkTitle":title, "nkMessage":content}),
      dataType: "json",
      success: function (res) {
        if (res.success) {
          //清楚表，数据然后，调用ajax请求查询一次新数据
          $("#tb_data").empty();
          queryAllNews();
        } else {
          alert(res.msg);
        }
      },
      error: function (res) {
        alert("请求出错");
      }
    });
  }

  /**
   * 删除选中的item
   */
  function deleteNewsItem(element) {
    //修改的id号
    var nkId = element.getAttribute("id");
    $.ajax({
      type: "GET",
      url: serverUrl + "/news/deleteNews?NkId=" + nkId,
      dataType: "json",
      success: function (res) {
        if (res.success) {
          //清楚表，数据然后，调用ajax请求查询一次新数据
          $("#tb_data").empty();
          queryAllNews();
        }
      },
      error: function (res) {
        alert("请求出错");
      }
    });


  }

  /**
   * 查看单条新闻的详细内容
   */
  function queryNews(element) {
    var nkId = element.getAttribute("id");
    //获取到标题标签和项内容的textarea
    $.ajax({
      type: "GET",
      url: serverUrl + "/news/queryNewsById?NkId=" + nkId,
      dataType: "json",
      success: function (res) {
        $("#new_title").text(res.data.nkTitle);
        $("#example-textarea-show").val(res.data.nkMessage);
      },
      error: function (res) {
        alert("请求出错");
      }
    })
  }
  function queryNews2(element) {
    var nkId = element.getAttribute("id");
    //获取到标题标签和项内容的textarea
    $.ajax({
      type: "GET",
      url: serverUrl + "/news/queryNewsById?NkId=" + nkId,
      dataType: "json",
      success: function (res) {
        $("#text-updata").val(res.data.nkTitle);
        $("#text-updata").attr("name", nkId);

        $("#textarea-updata").val(res.data.nkMessage);
      },
      error: function (res) {
        alert("请求出错");
      }
    })
  }
</script>
</body>
</html>
